//
//  ILGTestVC.m
//  ILGDemo
//
//  Created by happy on 2020/3/16.
//  Copyright © 2020 Happy. All rights reserved.
//

#import "ILGTestVC.h"

@interface ILGTestVC () <UITableViewDelegate, UITableViewDataSource>
{
    MJRefreshBackNormalFooter *_footer;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ILGTestVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initData];
    [self createUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewSafeAreaInsetsDidChange
{
    [super viewSafeAreaInsetsDidChange];
    
    if (@available(iOS 11.0, *)) {
        if (_footer) {
            _footer.ignoredScrollViewContentInsetBottom = self.view.safeAreaInsets.bottom;
        }
    }
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
}


#pragma mark - data

- (void)initData {
    self.dataArr = [@[@"1", @"2", @"3", @"4", @"5", @"6"] mutableCopy];
}


#pragma mark - UI
static NSString *kCellIdentifier = @"cellIdentifier";

- (void)createUI {
    self.title = @"Learn TableView";
//    self.ilg_isTabBarHidded = YES;
    
    [self createUIForTestTableView];
}

- (void)createUIForTestTableView
{
    [self setupTableView];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeightForStatusBarAndNavigationBar, self.view.ilg_width, 50)];
    topView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:topView];
    
    self.tableView.contentInset = UIEdgeInsetsMake(topView.ilg_height, 0, 0, 0);
}

- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier];
    tableView.tableFooterView = [UIView new];
    tableView.estimatedRowHeight = 44;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadFirstPage:)];
    tableView.mj_header = header;
    
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore:)];
    tableView.mj_footer = footer;
    _footer = footer;
}


#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
//    cell.contentView.backgroundColor = [UIColor greenColor];
    cell.backgroundColor = [UIColor redColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [NSString stringWithFormat:@"我是第%@行", self.dataArr[indexPath.row]];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"scrollViewDidScroll contentOffset = %@", NSStringFromCGPoint(scrollView.contentOffset));
}


#pragma mark - event

- (void)navigationBackButtonDidTap
{
    [super navigationBackButtonDidTap];
}

- (void)buttonClicked:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadFirstPage:(MJRefreshNormalHeader *)header
{
    [header endRefreshing];
    self.dataArr = [@[@"1", @"2", @"3", @"4", @"5", @"6"] mutableCopy];
    [self.tableView reloadData];
}

- (void)loadMore:(MJRefreshFooter *)refreshFooter {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
        
        NSMutableArray *tmpArr = [NSMutableArray array];
        for (int i = self.dataArr.count; i < self.dataArr.count+20; i++) {
            NSString *str = [NSString stringWithFormat:@"%d", i];
            [tmpArr addObject:str];
        }
        
        [self.dataArr addObjectsFromArray:tmpArr];
        [self.tableView reloadData];
    });
}

#pragma mark - other


@end
